<!--
- Author:   .
- Date:    2017/7/11 0011.
- File:    list.
-->
<template>
  <div>
    <div class="Web_Box pt88">
      <!--<div class="gd lt0 wauto z99">-->
        <!--<div class="Tabs2">-->
          <!--<ul>-->
            <!--<li :class="{'on': orderType == '1'}" style="width: 50%" @click="switchOrderType('1')">普通商品</li>-->
            <!--<li :class="{'on': orderType == '4'}" style="width: 50%" @click="switchOrderType('4')">积分商品</li>-->
          <!--</ul>-->
        <!--</div>-->
      <!--</div>-->
      <div class="gd lt0 wauto z99">
        <div class="Tabs2">
          <ul>
            <li :class="{'on': type == 0}" style="width: 20%" @click="switchType(0)">全部</li>
            <li :class="{'on': type == 1}" style="width: 20%" @click="switchType(1)">待付款</li>
            <li :class="{'on': type == 2}" style="width: 20%" @click="switchType(2)">待发货</li>
            <li :class="{'on': type == 3}" style="width: 20%" @click="switchType(3)">待收货</li>
            <li :class="{'on': type == 4}" style="width: 20%" @click="switchType(4)">已完成</li>
          </ul>
        </div>
      </div>
      <div class="DistributionOrder">
        <div class="ProList pt10">
          <div class="SearchBox p20 bg_efeff4" v-show="false">
            <div class="SearchBor xd hidden"><i class="iconfont icon-search lh70"></i>
              <input type="text" class="SearchInp pl80 f26" placeholder="搜索您需要的订单" v-model="keyword">
              <button type="button" class="SearchBut" @click="searchOrder">搜索</button>
            </div>
          </div>
          <ul
            v-infinite-scroll="loadMoreOrder"
            infinite-scroll-disabled="false"
            infinite-scroll-distance="0"
            infinite-scroll-immediate-check="false">
            <li v-for="order in orderList.data">
              <div class="tit"><span class="fl">订单号：{{order.order_info.order_sn}}</span>
                <span v-if="!order.order_info.if_lock" class="fr cr_595757">{{order.order_info.state_desc}}</span>
                <span v-else class="fr cr_595757">退款/退货订单</span>
              </div>
              <div style="margin-bottom: 10px;" class="giftbox f-cb" v-for="goods in order.order_info.extend_order_goods"  @click="gotoOrderDetail(order.order_info.order_id)">
                <div class="Pic">
                  <img :src="$config.website + $config.goodsImgSrc + goods.goods_image" alt=""/>
                </div>
                <div class="Con" style="width:11.5rem;">
                  <div class="">
                    <h2 class="">{{goods.goods_name}}&nbsp;&nbsp;<span v-for="attr in goods.goods_spec">{{attr.name}} </span></h2>
                  </div>
                  <div class="">
                    <span class="Prices"><span v-show="orderType == '1'">￥</span>{{goods.goods_price}}<span v-show="orderType == '4'">红积分</span></span>
                    <!--<span class="Prices" v-show="goods.gc_id == '1038'"><i class="iconfont icon-zengpin1"></i></span>-->
                    <span class="Num fr">×{{goods.goods_num}}</span>
                  </div>
                </div>
              </div>
              <div class="Operation">
                <!--<div class="Deductible" v-show="memberInfo.is_brush_man == '1' && order.order_info.is_needship == '0'">获得积分:{{parseFloat(order.order_info.get_freeze_balance || 0) + parseFloat(order.order_info.order_amount)}}</div>-->
                <p class="Total tr">共{{countOrderGoodsNum(order)}}件商品&nbsp;&nbsp;合计:<span v-show="orderType == '1'">￥</span>{{order.order_info.order_amount || 0}}<span v-show="orderType == '4'">红积分</span><span style="margin-left: 10px">实际付款：￥{{order.order_info.pay_amount}}</span></p>
                <span class=" pt10 disib f22 fl" v-show="order.order_info.is_needship == '0'">无需寄货</span>
                <span class=" pt10 disib f22 fl" v-show="order.order_info.is_needship == '1'">需要寄货</span>
                <div class="fr">
                  <!--<span class="but1 cr_595757 fr ml20" v-show="!order.order_info.if_lock && order.order_info.order_state == $dictionary.orderStatus.payed" @click="addRefundMoney(order.order_info.order_id, order.order_info.extend_order_goods[0].goods_id)">申请退款 </span>-->
                  <span class="but1 cr_45a73a fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.unpay" @click="pay(order)">立即付款 </span>
                  <span class="but1 cr_595757 fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.unpay" @click="cancleOrder(order.order_info.order_id)">取消订单 </span>
                  <span class="but1 cr_45a73a fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.sended" @click="confirmReceived(order.order_info.order_id)">确认收货 </span>
                  <!--<span class="but1 cr_595757 fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.sended" @click="gotoOrderExpress(order.order_info.order_id)">查看物流 </span>-->
                  <!--<span class="but1 fr cr_45a73a ml20" v-show="!order.order_info.if_lock && order.order_info.order_state == $dictionary.orderStatus.recevied && order.order_info.evaluation_state == 0" @click="addComment(order.order_info.order_id)">立即评价 </span>-->
                </div>
              </div>
            </li>
          </ul>
          <loading-more :allLoaded="orderList.allLoaded" :show="orderList.data.length > 0"></loading-more>
        </div>
      </div>
    </div>
    <empty-data :show="orderList.allLoaded && orderList.data.length <= 0" style="margin-top: 1rem"></empty-data>
    <footer-tab></footer-tab>
  </div>
</template>

<script>
  import {MessageBox} from 'mint-ui'
  import payPop from '../../components/payPop'
  import {mapModules, mapRules} from 'vuet'

  export default{
    mixins: [
      mapModules({memberInfo: 'memberInfo'}),
      mapRules({once: ['memberInfo']})
    ],
    data() {
      return {
        orderType: this.$route.query.orderType || '1',
        type: this.$route.query.type || 0, // 默认0，全部类型
        keyword: '', // 搜索关键字
        orderList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
      }
    },
    components: {},
    created() {
      this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
    },
    watch: {
      $route () {
        this.type = this.$route.query.type
        this.initOrderList()
        this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
      }
    },
    methods: {
      switchOrderType(value) {
        this.orderType = value
        this.orderList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
        this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
      },
      countOrderGoodsNum(order) {
        let count = 0
        if (!order.order_info || !order.order_info.extend_order_goods) {
          return count
        }
        for (let i = 0; i < order.order_info.extend_order_goods.length; i++) {
          count += parseInt(order.order_info.extend_order_goods[i].goods_num)
        }
        return count
      },
      gotoOrderExpress(id) {
        this.$router.push({name: 'order-express', params: {id: id}})
      },
      gotoOrderDetail(id) {
        this.$router.push({name: 'order-detail', params: {id: id}, query: {orderType: this.orderType}})
      },
      switchType(value) {
        this.$router.replace({name: 'order-list', query: {type: value, orderType: this.orderType}})
      },
      initOrderList() {
        this.orderList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
      },
      getOrderList(type, page, pageSize) {
        this.orderList.loading = true
        this.$request.getOrderList(type, page, pageSize, this.orderType).then(data => {
          if (data.length < this.orderList.pageSize) {
            this.orderList.allLoaded = true
          }
          this.orderList.data = [...this.orderList.data, ...data]
        }).finally(() => {
          this.orderList.requested = true
          this.orderList.loading = false
        })
      },
      loadMoreOrder() {
        if (this.orderList.loading || this.orderList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getOrderList(this.type, ++this.orderList.page, this.orderList.pageSize)
        }, 500)
      },
      cancleOrder(id) {
        MessageBox.confirm(`你确定要取消订单吗?`).then(action => {
          this.$request.cancleOrder(id).then(data => {
            this.$toast('取消订单成功')
            this.initOrderList()
            this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
          }).catch(e => {
            this.$toast('取消订单失败：' + e.msg)
          })
        }).catch(() => {
        })
      },
      searchOrder() {
      },
      addComment(id) {
        this.$router.push({name: 'comment-add', params: {id: id}})
      },
      addRefundGoods(orderId, goodsId) {
        this.$router.push({name: 'refund-goods-add', params: {orderId: orderId, goodsId: goodsId}})
      },
      addRefundMoney(orderId, goodsId) {
        this.$router.push({name: 'refund-money-add', params: {orderId: orderId, goodsId: goodsId}})
      },
      confirmReceived(id) {
        this.$request.confirmReceived(id).then(data => {
          this.$toast('确认收货成功')
          for (let i = 0; i < this.orderList.data.length; i++) {
            if (this.orderList.data[i].order_info.order_id == id) {
              this.orderList.data.splice(i, 1)
              break
            }
          }
        }).catch(e => {
          this.$toast('确认收货失败：' + e.msg)
        })
      },
      pay(order) {
        let paySn = order.order_info.pay_sn
        payPop({paySn: paySn, orderId: order.order_info.order_id, orderType: this.orderType})
      }
    }
  }
</script>

<style>
  .but1 {
    display: inline-block;
    height: 1.3rem;
    line-height: 1.25rem;
    text-align: center;
    font-size: 0.6rem;
    border: #dcdddd solid 1px;
    border-radius: 0.125rem;
    padding: 0 0.375rem;
  }
</style>
